YtoMsg = {}
var p_appId;
var p_userId;
var p_popState;
var p_callback;
var websocket;
var p_msg_json;
YtoMsg.loadScript = function (url, callback) {
    var script = document.createElement("script");
    script.type = "text/javascript";
    if (typeof(callback) != "undefined") {
        if (script.readyState) {
            script.onreadystatechange = function () {
                if (script.readyState == "loaded" || script.readyState == "complete") {
                    script.onreadystatechange = null;
                    callback();
                }
            };
        } else {
            script.onload = function () {
                callback();
            };
        }
    }
    script.src = url;
    document.head.appendChild(script);
}

YtoMsg.init = function (domain, appId, userId) {
    if (p_popState) {
        YtoMsg.loadScript(domain + '/resources/scripts/yto_msg.pop.js', function () {
            var ytoMsgHtml = "<div id=\"yto-msg-pop\" style='display: none'>" +
                "<style type=\"text/css\">\n" +
                "        #yto-msg-pop * {margin:0;padding:0;}\n" +
                "        #yto-msg-pop{background:#fff;width:260px;border:1px solid #e0e0e0;font-size:12px;position: fixed;right:10px;bottom:10px;z-index:9999}\n" +
                "        #yto-msg-popHead{line-height:32px;background:#f6f0f3;border-bottom:1px solid #e0e0e0;position:relative;font-size:12px;padding:0 0 0 10px;}\n" +
                "        #yto-msg-popHead h2{font-size:14px;color:#666;line-height:32px;height:32px;}\n" +
                "        #yto-msg-popHead #yto-msg-popClose{position:absolute;right:10px;top:1px;}\n" +
                "        #yto-msg-popHead a #yto-msg-popClose:hover{color:#f00;cursor:pointer;}\n" +
                "        #yto-msg-popContent{padding:5px 10px;}\n" +
                "        #yto-msg-popTitle a{line-height:24px;font-size:14px;font-family:'微软雅黑';color:#333;font-weight:bold;text-decoration:none;}\n" +
                "        #yto-msg-popTitle a:hover{color:#f60;}\n" +
                "        #yto-msg-popIntro{text-indent:24px;line-height:160%;margin:5px 0;color:#666;}\n" +
                "        #yto-msg-popMore{text-align:right;border-top:1px dotted #ccc;line-height:24px;margin:8px 0 0 0;}\n" +
                "        #yto-msg-popMore a{color:#f60;}\n" +
                "        #yto-msg-popMore a:hover{color:#f00;}\n" +
                "    </style>\n" +
                "    <div id=\"yto-msg-popHead\">\n" +
                "        <a id=\"yto-msg-popClose\" title=\"关闭\">关闭</a>\n" +
                "        <h2>温馨提示</h2>\n" +
                "    </div>\n" +
                "    <div id=\"yto-msg-popContent\">\n" +
                "        <dl>\n" +
                "            <dt id=\"yto-msg-popTitle\"><a href=\"\" target=\"_blank\">这里是参数</a></dt>\n" +
                "            <dd id=\"yto-msg-popIntro\">这里是内容简介</dd>\n" +
                "        </dl>\n" +
                "        <p id=\"yto-msg-popMore\"><a href=\"\" target=\"_blank\">查看 »</a></p>\n" +
                "    </div></div>\n";
            $('body').append(ytoMsgHtml);
        });
    }

    YtoMsg.loadScript(domain + '/resources/scripts/reconnecting-websocket.min.js', function () {
        var host = domain.split("//")[1];
        if ('WebSocket' in window) {
            websocket = new ReconnectingWebSocket("ws://" + host + "/websocket", null, {debug: true, maxReconnectAttempts: 4});
        } else if ('MozWebSocket' in window) {
            websocket = new MozWebSocket("ws://" + host + "/websocket");
        } else {
            websocket = new SockJS("http://" + host + "/sockjs/websocket");
        }
        var map = {
            "yto_msg_appId": appId,
            "yto_msg_userId": userId
        };
        websocket.onopen = function (evt) {
            websocket.send(JSON.stringify(map));
        };

        //用于服务器回调
        websocket.onmessage = function (evt) {
            var msg_json = eval("(" + evt.data + ")");
            p_msg_json = msg_json;
            p_callback(msg_json);
        };
    });
}
//注册客户端
YtoMsg.addMsg = function (domain, appId, userId, popState) {
    p_appId = appId;
    p_userId = userId;
    if (popState != null && popState == true) {
        p_popState = true;
    } else {
        p_popState = false;
    }
    if (domain.endWith("/")) {
        domain = domain.substr(0, domain.length - 1);
    }
    YtoMsg.init(domain, appId, userId);

}
//判断结尾字符
String.prototype.endWith = function (endStr) {
    var d = this.length - endStr.length;
    return (d >= 0 && this.lastIndexOf(endStr) == d)
}


//修改消息状态
YtoMsg.modifyState = function (msgIds) {
    var map = {
        "yto_msg_appId": p_appId,
        "yto_msg_userId": p_userId,
        "msgIds": msgIds,
        "interfaceName": "modifyMsgState"
    }
    websocket.send(JSON.stringify(map));
}

//设置回调函数，用于返回消息
YtoMsg.setCallback = function (callback) {
    p_callback = callback;
}

YtoMsg.pop = function (title, url, intro) {
    if (p_popState) {
        var resultFlag = p_msg_json.resultFlag;
        var resultMessage = p_msg_json.resultMessage;
        var datas = p_msg_json.datas;
        var info = "";
        if (resultFlag == true) {
            for (var i = 0; i < datas.length; i++) {
                info += datas[i].yto_msg_msgInfo + "</p>";
            }
        }
        if (title == null) {
            title = resultMessage;
        }
        if (url == null) {
            url = "";
        }
        if (intro == null) {
            intro = info;
        }

        new Pop(title, url, intro);
    } else {
        alert("请将YtoMsg.addMsg中的popState参数设置成true");
    }
}
